package com.baidu.redis.config;

import com.baidu.redis.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.exceptions.JedisDataException;
import io.rebloom.client.Client;

import java.io.Serializable;

/**
 * @auther zzyy
 * @create 2021-04-29 19:04
 */

@Configuration
@Slf4j
public class RedisConfig
{
    /**
     * @param lettuceConnectionFactory
     * @return
     *
     * redis序列化的工具配置类，下面这个请一定开启配置
     * 127.0.0.1:6379> keys *
     * 1) "ord:102"  序列化过
     * 2) "\xac\xed\x00\x05t\x00\aord:102"   野生，没有序列化过
     */
    @Bean
    public RedisTemplate<String,Serializable> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory)
    {
        RedisTemplate<String,Serializable> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //设置key序列化方式string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化方式json
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }


    // 从application文件中引入配置信息
    @Value("${redis.bloom.url}")
    private String rebloomUrl;
    @Value("${redis.bloom.port}")
    private Integer rebloomPort;
    @Value("${redis.bloom.port2}")
    private Integer rebloomPort2;
    @Value("${redis.bloom.init-capacity}")
    private Integer rebloomInitCapacity;
    @Value("${redis.bloom.error-rate}")
    private Double rebloomErrorRate;
    //布隆过滤器相关
    @Bean("rebloomClient")
    public Client rebloomClient() {
        log.info("rebloomUrl:{},rebloomPort:{},rebloomInitCapacity:{},rebloomErrorRate:{}",rebloomUrl,rebloomPort,rebloomInitCapacity,rebloomErrorRate);
        Client client = new Client(rebloomUrl, rebloomPort);
        // 创建一个新的bloom过滤器
        try{
            //BF.RESERVE {key} {error_rate} {capacity}
            client.createFilter(Constants.REBLOOM, rebloomInitCapacity, rebloomErrorRate);
        }catch (JedisDataException e){
            log.warn("已存在{}",e.getMessage());
        }
        return client;
    }
    @Bean("rebloomClient2")
    public Client rebloomClient2() {
        log.info("rebloomUrl:{},rebloomPort:{},rebloomInitCapacity:{},rebloomErrorRate:{}",rebloomUrl,rebloomPort2,rebloomInitCapacity,rebloomErrorRate);
        Client client = new Client(rebloomUrl, rebloomPort2);
        // 创建一个新的bloom过滤器
        try{
            //BF.RESERVE {key} {error_rate} {capacity}
            client.createFilter(Constants.REBLOOM, rebloomInitCapacity, rebloomErrorRate);
        }catch (JedisDataException e){
            log.warn("已存在{}",e.getMessage());
        }
        return client;
    }
}

